package com.example.show_auto.data

import com.google.gson.annotations.SerializedName

/**
 * 游戏数据模型
 */
data class GameData(
    val recipes: List<Recipe> = emptyList(),
    val chefs: List<Chef> = emptyList(),
    val equips: List<Equip> = emptyList(),
    val guests: List<Guest> = emptyList(),
    val materials: List<Material> = emptyList(),
    val condiments: List<Condiment> = emptyList(),
    val skills: List<Skill> = emptyList(),
    val quests: List<Quest> = emptyList(),
    // 新增：心法盘数据
    val ambers: List<Amber> = emptyList(),
    // 新增：装修数据
    val decorations: List<Decoration> = emptyList()
)

/**
 * 菜谱数据模型
 */
data class Recipe(
    @SerializedName("recipeId") val recipeId: String = "",
    @SerializedName("galleryId") val galleryId: String = "",
    val name: String = "",
    val rarity: Int = 0,
    val stirfry: Int = 0,
    val boil: Int = 0,
    val knife: Int = 0,
    val fry: Int = 0,
    val bake: Int = 0,
    val steam: Int = 0,
    val price: Int = 0,
    val time: Int = 0,
    val origin: String = "",
    val materials: List<RecipeMaterial> = emptyList(),
    val guests: List<RecipeGuest> = emptyList(),
    val condiment: String = "",
    val gift: String = "", // 符文奖励，"-"表示无奖励
    
    // 个人数据
    var got: Boolean = false,
    var rank: Int = 0
) {
    /**
     * 获取星级显示文本
     */
    fun getRarityStars(): String = "★".repeat(rarity)
    
    /**
     * 获取技法要求文本
     */
    fun getSkillRequirements(): String {
        val skills = mutableListOf<String>()
        if (stirfry > 0) skills.add("炒$stirfry")
        if (boil > 0) skills.add("煮$boil")
        if (knife > 0) skills.add("切$knife")
        if (fry > 0) skills.add("炸$fry")
        if (bake > 0) skills.add("烤$bake")
        if (steam > 0) skills.add("蒸$steam")
        return skills.joinToString(" ")
    }
    
    /**
     * 获取贵客列表文本
     */
    fun getGuestsText(): String {
        return guests.joinToString(", ") { it.guest }
    }
    
    /**
     * 获取升阶贵客列表
     */
    fun getRankGuests(): List<String> {
        // 由于真实数据中贵客没有星级信息，我们根据品阶返回所有贵客
        return when (rank) {
            0 -> emptyList() // 未制作
            1, 2, 3, 4 -> guests.map { it.guest } // 所有品阶都能招待所有贵客
            else -> emptyList()
        }
    }
    
    /**
     * 检查是否包含指定贵客
     */
    fun hasGuest(guestName: String): Boolean {
        return guests.any { it.guest == guestName }
    }

    /**
     * 检查升阶后是否包含指定贵客（且该贵客未完成）
     */
    fun hasRankGuest(guestName: String): Boolean {
        return getRankGuests().contains(guestName)
    }

    /**
     * 检查菜谱是否包含指定贵客（开业或升阶，避免重复计算）
     */
    fun containsGuest(guestName: String): Boolean {
        return guests.any { it.guest == guestName }
    }

    /**
     * 检查指定贵客是否处于未完成状态（用于筛选查询）
     * @param guestName 贵客名称
     * @return true表示该贵客未完成，菜谱应该被查询出来
     */
    fun hasUncompletedGuest(guestName: String): Boolean {
        // 注意：当前数据结构中，guests字段存储的是升阶贵客，不是开业贵客
        // 所以我们只检查升阶贵客的完成状态

        // 检查升阶贵客
        if (guests.isEmpty()) {
            return false // 没有升阶贵客的菜谱不显示
        }

        // 检查该贵客是否有任何未完成的升阶位置
        var hasUncompletedPosition = false

        for (i in guests.indices) {
            if (guests[i].guest == guestName) {
                // 升阶贵客的逻辑：
                // i=0 对应优级升阶贵客，当前品阶必须 < 2（还没达到优级）才可用
                // i=1 对应特级升阶贵客，当前品阶必须 < 3（还没达到特级）才可用
                // i=2 对应神级升阶贵客，当前品阶必须 < 4（还没达到神级）才可用
                val targetRank = i + 2 // 该贵客对应的目标品阶
                val isCompleted = rank >= targetRank

                if (!isCompleted) {
                    hasUncompletedPosition = true
                    break // 找到一个未完成的位置就足够了
                }
            }
        }

        return hasUncompletedPosition
    }

    /**
     * 获取指定贵客的未完成品级列表
     * @param guestName 贵客名称
     * @return 未完成的品级列表，如["优", "神"]
     */
    fun getUncompletedRanksForGuest(guestName: String): List<String> {
        val uncompletedRanks = mutableListOf<String>()

        // 检查升阶贵客（不包括开业贵客）
        if (guests.isNotEmpty()) {
            for (i in guests.indices) {
                if (guests[i].guest == guestName) {
                    // 根据位置确定品级名称，并检查是否未完成
                    val rankName = when (i) {
                        0 -> null // 开业贵客，不显示品级
                        1 -> "优"
                        2 -> "特"
                        3 -> "神"
                        else -> null
                    }

                    // 只有当前品阶还不能招待这个贵客时，才算未完成
                    if (rankName != null && i >= rank) {
                        uncompletedRanks.add(rankName)
                    }
                }
            }
        }

        return uncompletedRanks
    }

    /**
     * 获取带品级信息的菜谱显示名称
     * @param guestName 贵客名称
     * @return 格式为"菜谱名-优、神"的显示名称
     */
    fun getDisplayNameWithRanks(guestName: String): String {
        val uncompletedRanks = getUncompletedRanksForGuest(guestName)
        return if (uncompletedRanks.isNotEmpty()) {
            "$name-${uncompletedRanks.joinToString("、")}"
        } else {
            name
        }
    }
}

/**
 * 菜谱材料
 */
data class RecipeMaterial(
    val material: String = "",
    val quantity: Int = 0
)

/**
 * 菜谱贵客
 */
data class RecipeGuest(
    val guest: String = "",
    val rarity: Int = 0
)

/**
 * 厨师数据模型
 */
data class Chef(
    @SerializedName("chefId") val chefId: String = "",
    @SerializedName("galleryId") val galleryId: String = "",
    val name: String = "",
    val rarity: Int = 0,
    var stirfry: Int = 0,
    var boil: Int = 0,
    var knife: Int = 0,
    var fry: Int = 0,
    var bake: Int = 0,
    var steam: Int = 0,
    val skill: Int = 0,
    val ultimateSkillList: List<Int> = emptyList(), // 修炼技能ID列表
    val ultimateGoal: List<Int> = emptyList(), // 修炼任务ID数组
    val origin: String = "",
    val tags: List<Int> = emptyList(), // 性别标签：1=男性，2=女性
    
    // 采集点字段
    val meat: Int = 0,          // 肉类采集点
    val fish: Int = 0,          // 鱼类采集点
    val veg: Int = 0,           // 蔬菜采集点
    val creation: Int = 0,      // 面类采集点
    val diskDesc: String = "",  // 心法盘配置描述，如"蓝|绿|绿<br>最高5级"
    
    // 个人数据
    var got: Boolean = false,
    var rank: Int = 0,
    var ult: Boolean = false // 是否已修炼
) {
    /**
     * 获取第一个修炼技能ID
     */
    fun getFirstUltimateSkill(): Int {
        return ultimateSkillList.firstOrNull() ?: 0
    }
    
    /**
     * 获取所有修炼技能ID
     */
    fun getAllUltimateSkills(): List<Int> {
        return ultimateSkillList
    }
    
    /**
     * 检查是否有修炼技能
     */
    fun hasUltimateSkill(): Boolean {
        return ultimateSkillList.isNotEmpty()
    }
}

/**
 * 技能数据模型
 */
data class Skill(
    val skillId: Int = 0,
    val desc: String = "",
    val effect: List<SkillEffect> = emptyList()
)

/**
 * 技能效果数据模型
 */
data class SkillEffect(
    val condition: String = "",           // 条件类型：Partial, Next, Self, etc.
    val type: String = "",               // 效果类型：Fry, Stirfry, UseFry, UseStirfry, etc.
    val conditionType: String = "",      // 条件类型：CookbookRarity, etc.
    val value: Int = 0,                  // 效果数值
    val cal: String = "",                // 计算类型：Abs(绝对值), Percent(百分比)
    val rarity: Int = 0                  // 星级：用于UseAll和MaxEquipLimit类型
)

/**
 * 修炼任务数据模型
 */
data class Quest(
    val questId: Int = 0,
    val goal: String = "",
    val conditions: List<QuestCondition> = emptyList()
)

/**
 * 修炼任务条件
 */
data class QuestCondition(
    val skill: String = "", // 技法类型：knife, stirfry, boil, fry, bake, steam
    val rarity: Int = 0,    // 星级要求
    val rank: Int = 0,      // 品级要求：4=神级
    val num: Int = 0        // 总份数要求
)

/**
 * 厨具数据模型
 */
data class Equip(
    @SerializedName("equipId") val equipId: String = "",
    @SerializedName("galleryId") val galleryId: String = "",
    val name: String = "",
    val rarity: Int = 0,
    val skill: List<Int> = emptyList(),
    val origin: String = "",

    // 个人数据
    var got: Boolean = false
)

/**
 * 贵客数据模型
 */
data class Guest(
    val name: String = "",
    val gifts: List<GuestGift> = emptyList()
)

/**
 * 贵客礼物数据模型
 */
data class GuestGift(
    val recipe: String = "",
    val antique: String = ""
)

/**
 * 材料数据模型
 */
data class Material(
    @SerializedName("materialId") val materialId: String = "",
    val name: String = "",
    val rarity: Int = 0,
    val origin: String = ""
)

/**
 * 调料数据模型
 */
data class Condiment(
    @SerializedName("condimentId") val condimentId: String = "",
    val name: String = "",
    val skill: String = "",
    val origin: String = ""
)

/**
 * 心法盘（遗玉）数据模型
 */
data class Amber(
    @SerializedName("amberId") val amberId: String = "",
    @SerializedName("galleryId") val galleryId: String = "",
    val name: String = "",
    val type: Int = 0,          // 1=红 2=绿 3=蓝
    val rarity: Int = 0,
    val desc: String = "",
    val skill: List<Int> = emptyList(),
    val amplification: Int = 0, // 每级增加值
    val origin: String = ""
)

/**
 * 个人数据模型
 */
data class PersonalData(
    val recipes: Map<String, PersonalRecipe> = emptyMap(),
    val chefs: Map<String, PersonalChef> = emptyMap()
)

/**
 * 个人菜谱数据
 */
data class PersonalRecipe(
    val got: Boolean = false,
    val rank: Int = 0
)

/**
 * 个人厨师数据
 */
data class PersonalChef(
    val got: Boolean = false,
    val rank: Int = 0,
    val ult: Boolean = false, // 是否已修炼
    val equip: Int? = null, // 装备的厨具ID（可选字段）
    // 新增：心法盘等级与已穿戴遗玉ID列表
    val dlv: Int? = null,
    val ambers: List<Int>? = null
)

/**
 * 装修数据模型
 */
data class Decoration(
    val id: Int = 0,
    val icon: Int = 0,
    val name: String = "",
    val tipMin: Int? = null,
    val tipMax: Int? = null,
    val tipTime: Int? = null,
    val gold: Double = 0.0,
    val position: String = "",
    val suit: String = "",
    val suitGold: Double = 0.0,
    val origin: String = ""
)
